//
//  LXSlideSelectionAndTap.m
//  ibgs_patients
//
//  Created by xiao.zhao@lachesis-mh.com on 15/12/30.
//  Copyright © 2015年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXSlideSelectionAndTap.h"

#define TapSlideButtonBaseTag 1000

@interface LXSlideSelectionAndTap ()<UIScrollViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIScrollView *topScrollView;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIView *lineView;
@end

@implementation LXSlideSelectionAndTap

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setup];
    }
    
    return self;
}

-(void)setup
{
    _topScrollView = [[UIScrollView alloc]init];
    _topScrollView.backgroundColor = [UIColor whiteColor];
    _topScrollView.pagingEnabled = YES;
    _topScrollView.showsHorizontalScrollIndicator = NO;
    _topScrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:_topScrollView];
    
    _mainScrollView = [[UIScrollView alloc]init];
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.bounces = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.delegate = self;
    [self addSubview:_mainScrollView];
    
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = [UIColor colorWithHexString:@"cacaca"];
    [self addSubview:_lineView];
    
    self.textFont = [UIFont lx_systemFontOfSize:17.0f];
    self.maxEqualCount = 3;
    self.lineWidth = 2.0f;
    self.lineColor = kAppGreenTextColor;
    self.selectedIndex = 0;
}

#pragma mark - public Function
-(void)buildUI
{
    CGRect frect = CGRectZero;
    CGSize boundingSize = CGSizeMake(1000.0f, 100.0f);
    CGFloat startX = 0;
    CGFloat itemWidth = 0;
    
    for (int i = 0; i < _viewControllers.count; i++) {
        UIViewController *vc = [_viewControllers objectAtIndex:i];
        NSString *title = vc.title;
        
        itemWidth = [UIScreen mainScreen].bounds.size.width / _viewControllers.count;
        if (i == 0) {
            frect.origin.x = itemWidth - itemWidth / 2;
        } else {
            frect.origin.x = itemWidth;
        }
        
        frect.size.width = itemWidth / 2;
        frect.size.height = 40.0f;
        frect.origin.y = 0;
        UIButton *btn = [[UIButton alloc]initWithFrame:frect];
        [btn setTitleColor:kAppNormalTextColor forState:UIControlStateNormal];
        [btn setTitleColor:self.lineColor forState:UIControlStateSelected];
        [btn setTitle:title forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont lx_systemFontOfSize:19];
        [btn addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTag:TapSlideButtonBaseTag + i];
        [_topScrollView addSubview:btn];
        
        //main
        [_mainScrollView addSubview:vc.view];
    }
    
    frect = CGRectZero;
    frect.origin.x = 0;
    frect.origin.y = 40.0f;
    frect.size.width = itemWidth;
    frect.size.height = self.lineWidth;
    
    _topScrollView.contentSize = CGSizeMake(startX, _topScrollView.bounds.size.height);
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.bounds.size.width * _viewControllers.count, _mainScrollView.bounds.size.height);
}

-(void)scrollToIndex:(NSUInteger)index
{
    if(_delegate && [_delegate respondsToSelector:@selector(slideView:willShowPageAtIndex:)])
    {
        [_delegate slideView:self willShowPageAtIndex:index];
    }
    if(_delegate && [_delegate respondsToSelector:@selector(slideView:willLeavePageAtIndex:)])
    {
        [_delegate slideView:self willLeavePageAtIndex:self.lastPageIndex];
    }
    
    self.lastPageIndex = index;
    
    //取消之前的选中状态
    UIButton *previousBtn = (UIButton *)[_topScrollView viewWithTag:TapSlideButtonBaseTag + _selectedIndex];
    if(previousBtn)
    {
        [previousBtn setSelected:NO];
    }
    
    self.selectedIndex = index;
    
    UIButton *selBtn = (UIButton *)[_topScrollView viewWithTag:TapSlideButtonBaseTag + _selectedIndex];
    [selBtn setSelected:YES];
    
    NSString *title = selBtn.titleLabel.text;
    CGRect textRect = [title boundingRectWithSize:CGSizeMake(1000.0f, 100.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.textFont} context:NULL];
    
    CGRect btnFrame = selBtn.frame;
    
    CGRect animateRect = CGRectMake(_selectedIndex * _mainScrollView.bounds.size.width, 0, _mainScrollView.bounds.size.width, _mainScrollView.bounds.size.height);
    
    [_mainScrollView scrollRectToVisible:animateRect animated:YES];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _topScrollView.frame = CGRectMake(0, 0, self.bounds.size.width, 44);
    _mainScrollView.frame = CGRectMake(0, CGRectGetMaxY(_topScrollView.frame), self.bounds.size.width, self.bounds.size.height - CGRectGetHeight(_topScrollView.frame));
    _lineView.frame = CGRectMake(0, _topScrollView.bounds.size.height, self.bounds.size.width, 0.5f);
    
    for (UIViewController *vc in _viewControllers) {
        NSUInteger index = [_viewControllers indexOfObject:vc];
        
        vc.view.frame = CGRectMake(_mainScrollView.bounds.size.width * index, 0, _mainScrollView.bounds.size.width, _mainScrollView.bounds.size.height);
    }
    
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.bounds.size.width * _viewControllers.count, _mainScrollView.bounds.size.height);
    
    [self scrollToIndex:_selectedIndex];
}

-(void)topAction:(UIButton *)btn
{
    NSUInteger tag = btn.tag - TapSlideButtonBaseTag;
    
    [self scrollToIndex:tag];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(scrollView == _mainScrollView)
    {
        NSUInteger pageIndex = (scrollView.contentOffset.x + 5.0f) / scrollView.bounds.size.width;
        
        [self scrollToIndex:pageIndex];
    }
}

@end
